"""
循环经济分析系统主程序
包含经济价值计算、环境效益评估和回收网络优化三大功能
"""

from economic import EconomicAnalyzer
from environmental import EnvironmentalAnalyzer
from optimization import OptimizationEngine
from visualization import CircularEconomyVisualizer
import argparse

def main():
    parser = argparse.ArgumentParser(description='循环经济分析系统')
    parser.add_argument('--product', type=str, required=True, help='产品类型')
    parser.add_argument('--quantity', type=float, required=True, help='产品数量')
    parser.add_argument('--region', type=str, required=True, help='地区')
    parser.add_argument('--visualize', action='store_true', help='生成可视化图表')
    parser.add_argument('--save-path', type=str, help='图表保存路径')
    
    args = parser.parse_args()
    
    # 初始化各分析模块
    econ_analyzer = EconomicAnalyzer()
    env_analyzer = EnvironmentalAnalyzer()
    opt_engine = OptimizationEngine()
    
    # 初始化可视化模块
    visualizer = CircularEconomyVisualizer()
    
    # 执行经济分析
    economic_value = econ_analyzer.calculate_value(args.product, args.quantity)
    print("\n=== 经济价值分析结果 ===")
    for k, v in economic_value.items():
        print(f"{k}: {v}")
    
    # 执行环境分析
    env_impact = env_analyzer.assess_impact(args.product, args.quantity)
    print("\n=== 环境影响评估结果 ===")
    for k, v in env_impact.items():
        print(f"{k}: {v}")
    
    # 执行优化分析
    optimization = opt_engine.optimize_network(args.product, args.quantity, args.region)
    print("\n=== 回收网络优化建议 ===")
    if isinstance(optimization['优化建议'], list):
        for item in optimization['优化建议']:
            print(item)
    else:
        print(optimization['优化建议'])
    
    # 可视化处理
    if args.visualize:
        print("\n正在生成可视化图表...")
        quantities = [args.quantity * 0.5, args.quantity, args.quantity * 1.5]
        
        # 经济价值图表
        visualizer.plot_value_analysis(
            args.product, 
            quantities,
            args.save_path and f"{args.save_path}_value.png"
        )
        
        # 环境影响图表
        visualizer.plot_environmental_impact(
            args.product,
            quantities,
            args.save_path and f"{args.save_path}_env.png"
        )
        
        # 优化结果图表
        visualizer.plot_optimization_results(
            optimization,
            args.save_path and f"{args.save_path}_opt.png"
        )
        
        print(f"可视化图表已{'保存到指定路径' if args.save_path else '显示'}")

if __name__ == "__main__":
    main()